//
//  YSPVisitorView.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/16.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

protocol YSPVisitorViewDelegate :NSObjectProtocol{
    func doWhenRegisterBtnDidClicked()
    func doWhenLoginBtnDidClicked()
}

class YSPVisitorView: UIView {
    
    //代理属性
    weak var delegate: YSPVisitorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }
    
    func setBackgroundImageAndLabel (imageName: String, text: String) {
        houseView.hidden = true
        coverView.hidden = true
        iconView.image = UIImage(named: imageName)
        textLbl.text = text
        
    }

    func setupUI() {
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(textLbl)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //autoResizing会影响autoLayout，如果要用autoLayout就要关闭autoResizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        textLbl.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //设置textLbl能换行
        textLbl.numberOfLines = 0
        
        //设置约束
        
        //转轮
        //centerX = self.centerX
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //centerY = self.centerY - 10
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //房子
        //centerX = self.centerX
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //centerY = self.centerY
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -45))
        
        //textLbl
        //centerX = self.centerX
        self.addConstraint(NSLayoutConstraint(item: textLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //top = houseView.button + 15
        self.addConstraint(NSLayoutConstraint(item: textLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        //width = 200
        addConstraint(NSLayoutConstraint(item: textLbl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200))
        
        //注册按钮
        //trailing = self.centerX - 10
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: -10))
        //top = textlbl.bottom + 10
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        //height = 35
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //width = 100
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //登录按钮
        //trailing = self.centerX + 10
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 10))
        //top = textlbl.bottom + 10
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        //height = 35
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //width = 100
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //coverView
        //约束上下左右等于父控件
        //top
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //bottom
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //leading
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //trailing
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
    }
    
    //MARK: - 按钮点击事件
    @objc private func registerBtnDidClicked() {
        delegate?.doWhenRegisterBtnDidClicked()
    }
    
    @objc private func loginBtnDidClicked() {
        delegate?.doWhenLoginBtnDidClicked()
    }
    
    //MARK: - 懒加载
    
    //转轮
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //小房子
    private lazy var houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //label
    private lazy var textLbl: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,看看这里有什么惊喜!"
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //添加按钮的点击事件
        button.addTarget(self, action: "registerBtnDidClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: "loginBtnDidClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //中间的遮罩
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //MARK: - 动画
    func stopAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 25
        
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    
    
    
}
